<template>
	<view class="container">
		<!-- 加载中 -->
		<view v-if="loading" class="loading">
			<text>加载中，请稍候...</text>
		</view>
		<view v-else>
			<view class="top-functions">
				<view class="icon-item" @click="huoyuan">
					<image src="../../static/fabu.png"></image>
					<text>发布货源</text>
				</view>
				<view class="icon-item">
					<image src="../../static/shuiyinxiangji.png"></image>
					<text @click="shuiyin">水印相机</text>
				</view>
				<view class="icon-item">
					<image src="../../static/saoyisao.png"></image>
					<text @click="sao">扫一扫</text>
				</view>
			</view>

			<!-- 认证状态显示区域 -->
			<view class="auth-container">
				<!-- 根据认证进度显示不同的提示 -->
				<!-- 1. 未实名认证 -->
				<view v-if="authStatus === 'needReal'" class="auth-text">
					<image src="../../static/sourceofgoods/zanwu.png" mode=""></image>
					<text>立即去完成认证</text>
					<button :disabled="isAuthenticated.value === '0'" class="auth-button"
						@click="goToAuthentication">实名认证</button>
				</view>
				<view v-else-if="authStatus === 'needCompany'" class="auth-text">
					<image src="../../static/sourceofgoods/zanwu.png" mode=""></image>
					<text>您已完成实名认证，立即去完成企业认证即可发布货源</text>
					<button  class="auth-button"
						@click="goToAuthenti">企业认证</button>
				</view>

				<view v-else class="auth-text">
					<view class="auth-text" v-if="du=='0'">
						<image src="../../static/sourceofgoods/zanwu.png" mode=""></image>
						<text>暂未有进行中运输数据</text>
					</view>
					<view class="goods-info" v-else>
						<!-- 货源信息内容 -->
						<view class="huowu" v-for="opt in goodsData">
							<view class="top">
								<view style="color:#007aff;">货源信息 {{opt.sourceid}}</view>
								<view :class="'goods-status ' +getStatusClass(opt.status)">
									{{ getStatusText(opt.status) }}
								</view>
							</view>
							<view>货物总量：{{opt.totalweight}}吨</view>
							<view>货物余量：{{opt.totalweights}}吨</view>
							<view style="color: #f00;">货物运价：{{opt.unitprice}}元/吨</view>
							<view>开始时间：{{opt.starttime}}</view>
						</view>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script setup>
	import {
		ref,
		onMounted,
		computed
	} from 'vue';
	import {
		useuser
	} from '../../stores/user';
	import {
		http
	} from '@/config/request.js'
	import "@/config/request.js";
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app'
	const user = useuser();
	const goodsData = ref([]);
	const du = ref('0');
	const loading = ref(true); // 默认在加载中
	// 获取货源状态的文本
	const getStatusText = (status) => {
		switch (status) {
			case 0:
				return '待充值';
			case 1:
				return '进行中';
			case 2:
				return '已结束';
			case 3:
				return '已暂停';
			default:
				return '待开始';
		}
	}
	// 获取状态的类名（用于设置样式）
	const getStatusClass = (status) => {
		switch (status) {
			case 0:
				return 'pending'; // 待充值
			case 1:
				return 'in-progress'; // 进行中
			case 2:
				return 'completed'; // 已结束
			case 3:
				return 'paused'; // 已暂停
			default:
				return 'not-started'; // 待开始
		}
	}
	// 响应式变量，用于存储用户认证状态
	const isAuthenticated = ref(''); // 实名认证
	const isAuthenticatedCompany = ref(''); // 营业执照认证
	const isAccountOpened = ref(''); // 开户行认证
	//法人认证
	const isLegalPersonAuthenticated = ref('');
	const quanxian = ref('0');
	// 添加计算属性，判断当前认证阶段
	const authStatus = computed(() => {
		if (isAuthenticated.value !== 1) return 'needReal'; // 需要实名认证
		if (isAuthenticatedCompany.value !== '1' || isLegalPersonAuthenticated.value !== '1' || isAccountOpened
			.value !== '1') return 'needCompany'; // 需要企业类认证
		return 'done'; // 已完成所有认证
	});

	//跳转实名认证
	const goToAuthentication = () => {
		console.log('点击了实名认证按钮');
		if (isAuthenticated.value === 0) {
			uni.showToast({
					title: '请耐心等待审核',
					icon: 'none',
					duration: 3000,	
			})
			return;
		} else {
			uni.navigateTo({
				url: '/pages/homepage/pageinfo/shimingrenzheng',
			});
		}
	}
	//跳转企业认证
	const goToAuthenti = () => {
	
				uni.navigateTo({
					url: '/pages/my/pageinfo/ruzhu',
				});
				
			
	}
	//获取货源信息
	const getGoodsInfo = async () => {
		console.log('获取货源信息');
		var duixiang = {
			driverid: user.user.id,
			status: 1
		}
		// 获取货源信息
		const res = await http({
			url: '/selectHuoYuanBYStatus',
			method: 'post',
			data: duixiang
		})
		// 检查 res.shuju 是否为有效数组
		if (res.shuju && Array.isArray(res.shuju)) {
			du.value = res.shuju.length; // 获取数组的长度
			goodsData.value = res.shuju; // 更新 goodsData
		} else {
			du.value = 0; // 如果 res.shuju 无效，设置 du 为 0
			goodsData.value = []; // 确保 goodsData 是一个空数组
		}
		console.log(goodsData.value);

	}

	const huoyuan = () => {
		if (authStatus.value === 'done') {
			uni.navigateTo({
				url: '/pages/sourceofgoods/pageinfo/fabu'
			});
		} else {
			let unAuthItems = [];
			if (isAuthenticated.value !== '1') unAuthItems.push('实名认证');
			if (isAuthenticatedCompany.value !== '1') unAuthItems.push('企业认证');
			if (isLegalPersonAuthenticated.value !== '1') unAuthItems.push('法人认证');
			if (isAccountOpened.value !== '1') unAuthItems.push('开户认证');

			uni.showToast({
				title: '请先完成：企业认证',
				icon: 'none',
				duration: 3000
			});
		}
	};


	const fetchAuthenticationStatuses = async () => {
		// 营业执照认证
		const res2 = await http({
			url: '/licenseAuthentication/select/' + user.user.id,
			method: 'GET'
		});
		isAuthenticatedCompany.value = res2.shuju ? res2.shuju.auditStatus : '0';

		// 法人认证
		const res3 = await http({
			url: '/legalAuthentication/select/' + user.user.id,
			method: 'GET'
		});
		isLegalPersonAuthenticated.value = res3.shuju ? res3.shuju.auditStatus : '0';

		// 开户行认证
		const res4 = await http({
			url: '/accountAuthentication/select/' + user.user.id,
			method: 'GET'
		});
		isAccountOpened.value = res4.shuju ? res4.shuju.auditStatus : '0';
		const res5 = await http({
			url: '/driverRealname/query/' + user.user.id,
			method: 'GET',
		});
		isAuthenticated.value = res5.shuju ? res5.shuju.status : '0';
		loading.value = false; // 认证查完，页面可以显示
		await getGoodsInfo(); // 再拉取货源
	}

	// 其他方法保持不变
	const shuiyin = () => {
		// 水印相机功能实现
	};

	const sao = () => {
		// 扫一扫功能实现
	};
	getGoodsInfo();
	const qianbao = async () => {
		const qianbao = await http({
			url: '/insertWallet/' + user.user.id,
			method: 'get',
		});
		console.log(qianbao)
	}

	onMounted(() => {
		// 获取用户认证状态
		console.log('实名认证状态')
		fetchAuthenticationStatuses();
		qianbao()

	});
</script>

<style lang="scss">
	/* 页面根元素 */
	.loading {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 80vh;
		font-size: 32rpx;
		color: #999;
	}

	page {
		height: 100%;
		background-color: #eeeeee;
		padding-bottom: 100rpx;
	}

	.container {
		background-color: #eeeeee;
		min-height: 100vh;
		box-sizing: border-box;
		padding: 15px;
		position: relative;


		.top-functions {
			display: flex;
			justify-content: space-around;
			margin-bottom: 20px;
			background-color: #ffffff;
			padding: 15px 0;
			border-radius: 10px;
		}

		.icon-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin: 10px;
		}

		.icon-item image {
			width: 48px;
			height: 48px;
		}

		.icon-item text {
			margin-top: 5px;
			text-align: center;
			font-size: 14px;
			color: #333;
		}

		.auth-container {
			width: 100%;
			background: linear-gradient(135deg, #f5f7fa 0%, #eee 100%);
			border-radius: 10px;
			padding: 20px;
			box-sizing: border-box;
			border-bottom: none;

		}

		.auth-text {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			margin-bottom: 10px;
			padding: 20px;
			height: 500rpx;
		}

		.auth-text image {
			width: 70px;
			height: 70px;
			margin-bottom: 100rpx;

		}

		.auth-text text {
			font-size: 25rpx;
			color: #333;
			margin-bottom: 20px;
			text-align: center;
		}

		.auth-button {
			background-color: #007AFF;
			color: white;
			border: none;
			font-size: 16px;
			width: 400rpx;
			height: 80rpx;
			line-height: 80rpx;
			margin-top: 10px;
			box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
			transition: background-color 0.3s ease;
		}

		.auth-button:hover {
			background-color: #005bb5;
		}

		.goods-info {
			width: 100%;
			height: 300rpx;
			margin-top: -300rpx;
			margin-bottom: 30rpx;

			/* 货源信息样式可以根据实际需求添加 */
			.huowu {
				width: 600rpx;
				font-size: 29rpx;
				color: #333;
				margin-bottom: 50rpx;
				padding: 20rpx;
				margin-left: -50rpx;
				background-color: #fff;
				border-radius: 30rpx;

				.top {
					display: flex;
					justify-content: space-between;
					align-items: center;
				}
			}

			.huowu view {
				font-size: 29rpx;
				margin-top: 10rpx;
				margin-bottom: 20rpx;
			}
		}

		/* 各个状态的颜色 */
		.goods-status {
			font-weight: bold;
			padding: 5px 10px;
			border-radius: 5px;
			text-align: center;
		}

		.goods-status.pending {

			color: #ff0000;
		}

		.goods-status.in-progress {
			color: #55ff7f;
		}

		.goods-status.completed {

			color: #ff0000;
		}

		.goods-status.paused {

			color: #7f8c8d;
		}

		.goods-status.not-started {

			color: #7f8c8d;
		}
	}
</style>